package fourfaith.com.ff_sl21_app;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import fourfaith.com.ff_sl21_app.utils.Constants;

public class VideoConfigActivity extends AppCompatActivity {

    private EditText etIpAddress,etPort;
    private Button okBtn,cancelBtn;
    private OnDialogClickListener dialogClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_config);
        initView();
        initListeners();
    }
    public void initView(){
        etIpAddress = (EditText)findViewById(R.id.etIpAddress);
        etPort = (EditText)findViewById(R.id.etPort);
        okBtn =(Button)findViewById(R.id.btn_ok);
        cancelBtn = (Button)findViewById(R.id.btn_cancel);
        okBtn.setOnClickListener(videoConfig);
        SharedPreferences sharedPreferences = getSharedPreferences(Constants._videoConfig, Context.MODE_PRIVATE);
        String ip = sharedPreferences.getString("videoIp", "");
        if(!ip.equals("")){
            etIpAddress.setText(ip);
            etPort.setText(sharedPreferences.getString("videoPort",""));
        }else{
            //默认值
            etIpAddress.setText(Constants.videoIp);
            etPort.setText(Constants.videoPort);
        }
    }
    private void initWindow() {
        //自定义windows弹窗的样式
        getWindow().setBackgroundDrawable(new ColorDrawable(0));
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN |
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    private View.OnClickListener videoConfig = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String _etIpAddress = etIpAddress.getText().toString();
            String _etPort = etPort.getText().toString();
            if (valid(_etIpAddress, _etPort)) {
                getVideoConfigSave(_etIpAddress, _etPort);
            }
        }
    };
    private boolean valid(String etIpAddress, String etPort) {

        if(!Constants.isNotEmpty(etIpAddress)){
            Toast.makeText(VideoConfigActivity.this, getString(R.string.prompt_ip) + "不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if(!Constants.isNotEmpty(etPort)){
            Toast.makeText(VideoConfigActivity.this, getString(R.string.prompt_port) +"不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }
    public void getVideoConfigSave(final String etIpAddress, final String etPort){
        //使用SharedPreferences存储数据
        SharedPreferences sharedPreferences = getSharedPreferences(Constants._videoConfig, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("videoIp",etIpAddress);
        editor.putString("videoPort", etPort);
        editor.commit();
        Toast.makeText(VideoConfigActivity.this, "视频参数配置成功", Toast.LENGTH_SHORT).show();
        finish();
        if (dialogClickListener != null) {
            dialogClickListener.onCancelClick();
        }
    }
    private void initListeners() {
        //取消按钮取点击监听
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭当前的activity
                finish();
                if (dialogClickListener != null) {
                    dialogClickListener.onCancelClick();
                }
            }
        });
    }

    public void setOnDialogClickListener(OnDialogClickListener clickListener){
        dialogClickListener = clickListener;
    }


    public interface OnDialogClickListener{
        void onOKClick();
        void onCancelClick();
    }

}
